{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 发送邮件<br>\n",
    "以QQ邮箱为例，进入qq邮箱后，打开设置->账户设置->找到下方方框并开启，获取授权码(<b>授权码只出现一次，务必保存好！</b>)\n",
    "![截图](邮箱.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 各个邮件smtp服务器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#需要注意的是，各个邮箱的smtp服务器是不一样的。\n",
    "比如\n",
    "#qq\n",
    "smtp.qq.com\n",
    "#163\n",
    "smtp.163.com\n",
    "#gmail\n",
    "smtp.gmail.com\n",
    "#126\n",
    "smtp.126.com"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 代码模板"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### try、except简单知识点补充\n",
    "如果此段程序没有用 try 包裹，又或者没有为该异常配置处理它的 except 块，则 Python 解释器将无法处理，**程序就会停止运行；**<br>\n",
    "反之，如果程序发生的异常经 try 捕获并由 except 处理完成，则**程序可以继续执行。**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import smtplib\n",
    "from email.mime.multipart import MIMEMultipart\n",
    "from email.mime.text import MIMEText\n",
    "from email.mime.image import MIMEImage\n",
    "from email.mime.application import MIMEApplication\n",
    "def send_email(receiver):\n",
    "    # 发送者邮箱地址\n",
    "    senderMail = '【发送者邮箱地址】'\n",
    "    # 发送者 QQ 邮箱授权码\n",
    "    authCode = '【你的邮箱授权码】'\n",
    "    \n",
    "    # 接收者邮箱地址\n",
    "    receiverMail =receiver\n",
    "    # 邮件主题\n",
    "    subject = 'xxx主题'\n",
    "    msgRoot = MIMEMultipart('related')\n",
    "    msgRoot['Subject'] = subject\n",
    "    msgRoot['From'] = senderMail\n",
    "    msgRoot['To'] = receiverMail\n",
    "    msgAtv = MIMEMultipart('alternative')\n",
    "    msgRoot.attach(msgAtv)\n",
    "    # html\n",
    "    html_content = '''\n",
    "    <a href=\"https://www.baidu.com\">百度URL</a>\n",
    "    <p>欢迎你选择这门课</p>\n",
    "    <p><img src=\"cid:image\"></p>\n",
    "    '''\n",
    "    html = MIMEText(html_content, 'html', 'utf-8')\n",
    "    msgAtv.attach(html)\n",
    "    # 发送图片\n",
    "    f = open('xxx.jpg', 'rb')\n",
    "    msgImage = MIMEImage(f.read())\n",
    "    f.close()\n",
    "    msgImage.add_header('Content-ID', '<image>')\n",
    "    msgRoot.attach(msgImage)\n",
    "    # 附件\n",
    "    #发送PPT\n",
    "    part1 = MIMEApplication(open(r'xxx.pptx', 'rb').read())\n",
    "    part1.add_header('Content-Disposition', 'attachment', filename=\"xxx.pptx\")  # 发送文件名称\n",
    "    msgRoot.attach(part1)\n",
    "    # 发送doc文件\n",
    "    part2 = MIMEApplication(open(r'xxx.doc', 'rb').read())\n",
    "    part2.add_header('Content-Disposition', 'attachment', filename=\"xxx.doc\")  # 发送文件名称\n",
    "    msgRoot.attach(part2)\n",
    "    #发送excel文件\n",
    "    part3 = MIMEApplication(open(r'xxx.xlsx', 'rb').read())\n",
    "    part3.add_header('Content-Disposition', 'attachment', filename=\"xxx.xlsx\")  # 发送文件名称\n",
    "    msgRoot.attach(part3)\n",
    "    #发送PDF文件\n",
    "    part4 = MIMEApplication(open(r'xxx.pdf', 'rb').read())\n",
    "    part4.add_header('Content-Disposition', 'attachment', filename=\"xxx.pdf\")  # 发送文件名称\n",
    "    msgRoot.attach(part4)\n",
    "    try:\n",
    "        # 发信服务器\n",
    "        smtp_server = 'smtp.qq.com'#根据邮箱平台相应的修改服务器\n",
    "        # 开启发信服务，这里使用的是加密传输\n",
    "        server=smtplib.SMTP_SSL(smtp_server) \n",
    "        server.connect(smtp_server,465)\n",
    "        # 登录发信邮箱\n",
    "        server.login(senderMail,authCode)\n",
    "        # 发送邮件\n",
    "        server.sendmail(senderMail,receiverMail, msgRoot.as_string())\n",
    "    except smtplib.SMTPException as e:\n",
    "        print(e)\n",
    "        print('邮件发送异常')\n",
    "    finally:\n",
    "        # 关闭服务器\n",
    "        server.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df_学员名单=pd.read_excel(\"(报名表)大数据时代下爬虫与数据分析及可视化学员名单.xlsx\")\n",
    "df_学员名单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{47.0: '1806992573@qq.com',\n",
       " 48.0: '15913226621@139.com',\n",
       " 49.0: '1743098622@qq.com',\n",
       " 50.0: '2292535116@qq.com',\n",
       " 51.0: '1075232880@qq.com',\n",
       " 52.0: '1678342820@qq.com',\n",
       " 53.0: '1984039719@qq.com',\n",
       " 54.0: '1595097410@qq.com',\n",
       " 55.0: '839449807@qq.com',\n",
       " 56.0: '1094388132@qq.com',\n",
       " 57.0: '15992344636@139.com',\n",
       " 58.0: '1365667339@qq.com',\n",
       " 59.0: '2312885241@qq.com',\n",
       " 60.0: '3498354338@qq.com',\n",
       " 61.0: '1365524838@qq.com',\n",
       " 62.0: '849976629@qq.com',\n",
       " 63.0: '2251309849@qq.com',\n",
       " 64.0: '2690684929@qq.com',\n",
       " 65.0: '757882393@qq.com',\n",
       " 66.0: '1045239782@qq.com',\n",
       " 67.0: 'sigua20011231@163.com',\n",
       " 68.0: '2062946759@qq.com',\n",
       " 69.0: 'zzy_6868@qq.com',\n",
       " 70.0: '1601994413@qq.com',\n",
       " 71.0: '2193533391@qq.com',\n",
       " 72.0: '1150990742@qq.con',\n",
       " 73.0: '1840935462@qq.com',\n",
       " 74.0: '1369423181@qq.com',\n",
       " 75.0: '1812840110@qq.com',\n",
       " 76.0: '814476648@qq.com',\n",
       " 77.0: 'yu13726925112@qq.com',\n",
       " 78.0: '1447049136@qq.com',\n",
       " 79.0: '2251961868@qq.com',\n",
       " 80.0: '867297003@qq.com',\n",
       " 81.0: '2364793443@qq.com',\n",
       " 82.0: '13673181@qq.com',\n",
       " 83.0: '1319072866@qq.com',\n",
       " 84.0: '961947088@qq.com',\n",
       " 85.0: '3411145442@qq.com',\n",
       " 86.0: '106034342@qq.com',\n",
       " 87.0: '996195032@qq.com',\n",
       " 88.0: '853891694@qq.com',\n",
       " 89.0: '13046249759@139.com',\n",
       " 90.0: '635115036@qq.com',\n",
       " 91.0: '2035895351@qq.com',\n",
       " 92.0: '2241724901@qq.com',\n",
       " 93.0: '2732473094@qq.com',\n",
       " 94.0: '1194070405@qq.com',\n",
       " 95.0: '2744993477@qq.com',\n",
       " 96.0: '1421547317@qq.com',\n",
       " 97.0: '1019986076@qq.com',\n",
       " 98.0: '1195741336@qq.com',\n",
       " 99.0: '1304718010@qq.com',\n",
       " 100.0: '1410385302@qq.com',\n",
       " 101.0: 'qingyun327@qq.com',\n",
       " 102.0: '2572676702@qq.com',\n",
       " 103.0: '646214179@qq.com',\n",
       " 104.0: 'isla_as@163.com',\n",
       " 105.0: '2724122174@qq.com',\n",
       " 106.0: '1573502969@qq.com',\n",
       " 107.0: '2962171950@qq.com',\n",
       " 108.0: 'yuyua321@outlook.com',\n",
       " 109.0: '2018099001@qq.com',\n",
       " 110.0: '411957243@qq.com',\n",
       " 111.0: '1954388812@qq.com',\n",
       " 112.0: '970723907@qq.com',\n",
       " 113.0: '2771841415@qq.com',\n",
       " 115.0: '1159914765@qq.com',\n",
       " 116.0: '1394784053@qq.com',\n",
       " 117.0: '1187519812@qq.com',\n",
       " 118.0: '3050228260@qq.com',\n",
       " 119.0: '465470638@qq.com',\n",
       " 120.0: '1640100693@qq.com',\n",
       " 121.0: '758898070@qq.com',\n",
       " 122.0: '1784282840@qq.com',\n",
       " 123.0: '1924047204@qq.com',\n",
       " 124.0: '1073534693@qq.com',\n",
       " 125.0: '3540307900@qq.com',\n",
       " 126.0: '877340449@qq.com',\n",
       " 127.0: '569900641@qq.com',\n",
       " 128.0: '3185347202@qq.com',\n",
       " nan: 'tblucaszy@163.com'}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "学员邮箱=df_学员名单.set_index(\"序号\")[\"邮箱\"].to_dict()\n",
    "学员邮箱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编号 47.0 邮箱号 1806992573@qq.com\n",
      "编号 48.0 邮箱号 15913226621@139.com\n",
      "编号 49.0 邮箱号 1743098622@qq.com\n",
      "编号 50.0 邮箱号 2292535116@qq.com\n",
      "编号 51.0 邮箱号 1075232880@qq.com\n",
      "编号 52.0 邮箱号 1678342820@qq.com\n",
      "编号 53.0 邮箱号 1984039719@qq.com\n",
      "编号 54.0 邮箱号 1595097410@qq.com\n",
      "编号 55.0 邮箱号 839449807@qq.com\n",
      "编号 56.0 邮箱号 1094388132@qq.com\n",
      "编号 57.0 邮箱号 15992344636@139.com\n",
      "编号 58.0 邮箱号 1365667339@qq.com\n",
      "编号 59.0 邮箱号 2312885241@qq.com\n",
      "编号 60.0 邮箱号 3498354338@qq.com\n",
      "编号 61.0 邮箱号 1365524838@qq.com\n",
      "编号 62.0 邮箱号 849976629@qq.com\n",
      "编号 63.0 邮箱号 2251309849@qq.com\n",
      "编号 64.0 邮箱号 2690684929@qq.com\n",
      "编号 65.0 邮箱号 757882393@qq.com\n",
      "编号 66.0 邮箱号 1045239782@qq.com\n",
      "编号 67.0 邮箱号 sigua20011231@163.com\n",
      "编号 68.0 邮箱号 2062946759@qq.com\n",
      "编号 69.0 邮箱号 zzy_6868@qq.com\n",
      "编号 70.0 邮箱号 1601994413@qq.com\n",
      "编号 71.0 邮箱号 2193533391@qq.com\n",
      "编号 72.0 邮箱号 1150990742@qq.con\n",
      "编号 73.0 邮箱号 1840935462@qq.com\n",
      "编号 74.0 邮箱号 1369423181@qq.com\n",
      "编号 75.0 邮箱号 1812840110@qq.com\n",
      "编号 76.0 邮箱号 814476648@qq.com\n",
      "编号 77.0 邮箱号 yu13726925112@qq.com\n",
      "编号 78.0 邮箱号 1447049136@qq.com\n",
      "编号 79.0 邮箱号 2251961868@qq.com\n",
      "编号 80.0 邮箱号 867297003@qq.com\n",
      "编号 81.0 邮箱号 2364793443@qq.com\n",
      "编号 82.0 邮箱号 13673181@qq.com\n",
      "编号 83.0 邮箱号 1319072866@qq.com\n",
      "编号 84.0 邮箱号 961947088@qq.com\n",
      "编号 85.0 邮箱号 3411145442@qq.com\n",
      "编号 86.0 邮箱号 106034342@qq.com\n",
      "{'106034342@qq.com': (550, b'Mailbox not found or access denied.')}\n",
      "邮件发送异常\n",
      "编号 87.0 邮箱号 996195032@qq.com\n",
      "编号 88.0 邮箱号 853891694@qq.com\n",
      "编号 89.0 邮箱号 13046249759@139.com\n",
      "编号 90.0 邮箱号 635115036@qq.com\n",
      "编号 91.0 邮箱号 2035895351@qq.com\n",
      "编号 92.0 邮箱号 2241724901@qq.com\n",
      "编号 93.0 邮箱号 2732473094@qq.com\n",
      "编号 94.0 邮箱号 1194070405@qq.com\n",
      "编号 95.0 邮箱号 2744993477@qq.com\n",
      "编号 96.0 邮箱号 1421547317@qq.com\n",
      "编号 97.0 邮箱号 1019986076@qq.com\n",
      "编号 98.0 邮箱号 1195741336@qq.com\n",
      "编号 99.0 邮箱号 1304718010@qq.com\n",
      "编号 100.0 邮箱号 1410385302@qq.com\n",
      "编号 101.0 邮箱号 qingyun327@qq.com\n",
      "编号 102.0 邮箱号 2572676702@qq.com\n",
      "编号 103.0 邮箱号 646214179@qq.com\n",
      "编号 104.0 邮箱号 isla_as@163.com\n",
      "编号 105.0 邮箱号 2724122174@qq.com\n",
      "编号 106.0 邮箱号 1573502969@qq.com\n",
      "编号 107.0 邮箱号 2962171950@qq.com\n",
      "编号 108.0 邮箱号 yuyua321@outlook.com\n",
      "编号 109.0 邮箱号 2018099001@qq.com\n",
      "编号 110.0 邮箱号 411957243@qq.com\n",
      "编号 111.0 邮箱号 1954388812@qq.com\n",
      "编号 112.0 邮箱号 970723907@qq.com\n",
      "编号 113.0 邮箱号 2771841415@qq.com\n",
      "编号 115.0 邮箱号 1159914765@qq.com\n",
      "编号 116.0 邮箱号 1394784053@qq.com\n",
      "编号 117.0 邮箱号 1187519812@qq.com\n",
      "编号 118.0 邮箱号 3050228260@qq.com\n",
      "编号 119.0 邮箱号 465470638@qq.com\n",
      "编号 120.0 邮箱号 1640100693@qq.com\n",
      "编号 121.0 邮箱号 758898070@qq.com\n",
      "编号 122.0 邮箱号 1784282840@qq.com\n",
      "编号 123.0 邮箱号 1924047204@qq.com\n",
      "编号 124.0 邮箱号 1073534693@qq.com\n",
      "编号 125.0 邮箱号 3540307900@qq.com\n",
      "编号 126.0 邮箱号 877340449@qq.com\n",
      "编号 127.0 邮箱号 569900641@qq.com\n",
      "编号 128.0 邮箱号 3185347202@qq.com\n",
      "编号 nan 邮箱号 tblucaszy@163.com\n"
     ]
    }
   ],
   "source": [
    "from random import random \n",
    "import time\n",
    "\n",
    "for k,v in 学员邮箱.items():\n",
    "    #使邮件发送的慢一点\n",
    "    time.sleep(5+random()*8)\n",
    "    print('编号',k,'邮箱号',v)\n",
    "    send_email(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 发送一封邮件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "send_email('xxx@126.com')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
